<table id="operation_cp_manage" title="" class="easyui-datagrid" style="width: 100%" url="/admin/operation/cp_manage"
  toolbar="#operation-manage-datagrid-toolbar" pagination="true" pageSize="50" rownumbers="true" nowrap="false"
  checkOnSelect="false" singleSelect="false" pageList="[50,100,150,300]" tabPosition="bottom" fitColumns="true" fit="true"
  showFooter="true" striped="false" data-options="">
  <thead>
    <tr>
      <th data-options="field:'ck',checkbox:true"></th>
      <th data-options="field:'code',align:'center',width:100">单位编号</th>
      <th data-options="field:'name',align:'center',width:100">单位名称</th>
      <th data-options="field:'district',align:'center',width:150">所属区域</th>
      <th data-options="field:'address',align:'center',width:150">单位地址</th>
      <th data-options="field:'principal',align:'center',width:50">负责人</th>
      <th data-options="field:'phone',align:'center',width:50">负责电话</th>
      <th data-options="field:'ucc',align:'center',width:100">统一信用代码</th>
      <th data-options="field:'created_time',align:'center',width:100">登记日期</th>
      <th data-options="field:'company_id',align:'center',formatter:OperationCpManageModel.operate,width:100">管理员操作</th>
    </tr>
  </thead>
</table>
<style>
  label {
        margin-left: 8px;
    }
</style>
<div id="operation-manage-datagrid-toolbar" style="padding:5px;height:auto">
  <form style="margin:15px;">
    <label for="cp_keyword">关键字：</label> <input type="text" class="easyui-textbox" id="cp_keyword" style="margin-right: 5px">
    <a href="javascript:void(0);" onclick="OperationCpManageModel.search(this);" class="easyui-linkbutton" data-options="plain:false,iconCls:'icons-table-table'">查询</a>
    <a style="margin-left: 20px;" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:false,iconCls:'icons-table-table_add'"
      onclick="OperationCpManageModel.set_cpinfo()">新增</a>
    <a style="margin-left: 10px;" href="javascript:void(0);" class="easyui-linkbutton toolbar-btn__delete" data-options="plain:false,iconCls:'icons-table-table_delete'"
      onclick="OperationCpManageModel.batch_delete()">删除</a>

  </form>
  <a href="javascript:void(0);" class="easyui-linkbutton" style="margin-left: 20px;" data-options="plain:true,iconCls:'icons-table-table_refresh'"
    onclick="OperationCpManageModel.refresh()">刷新</a>
  <!--<a style="margin-left: 10px;" href="javascript:void(0);" class="easyui-linkbutton"-->
  <!--data-options="plain:false,iconCls:'icons-table-table_go'" onclick="OperationCpManageModel.export()">导入</a>-->
  <!--<a style="margin-left: 10px;" href="javascript:void(0);" class="easyui-linkbutton"-->
  <!--data-options="plain:false,iconCls:'icons-table-table_edit'" onclick="OperationCpManageModel.import()">导出</a>-->
</div>

<script type="text/javascript">

  var OperationCpManageModel = {
    dialog: '#globel-dialog-div',
    datagrid: '#operation_cp_manage',
    //操作格式化
    operate: function (val, arr) {
      if (val) {
        var btn = [];
        //        btn.push('<a href="javascript:void(0);" onclick="OperationCpManageModel.set_cpinfo(' + val + ')">添加</a>');
        btn.push('<a href="javascript:void(0);" onclick="OperationCpManageModel.set_cpinfo(' + val + ')">编辑</a>');
        btn.push('<a href="javascript:void(0);" onclick="OperationCpManageModel.delet(' + val + ')">删除</a>');
        return btn.join(' |  ');
      }
    },

    //刷新
    refresh: function () {
      $(this.datagrid).datagrid('reload');
    },

    //新增
    set_cpinfo: function (id) {
      var that = this;
      var href = "<{:U('admin/operation/set_cp_info')}>";
      if (id > 0) {
        href += href.indexOf('?') != -1 ? '&id=' + id : '?id=' + id;
      }
      $(that.dialog).dialog({
        title: '新增/编辑',
        iconCls: 'icons-application-application_add',
        width: 600,
        height: 400,
        cache: false,
        href: href,
        modal: true,
        collapsible: false,
        minimizable: false,
        resizable: false,
        maximizable: false,
        buttons: [{
          text: '确定',
          iconCls: 'icons-other-tick',
          handler: function () {
            $(that.dialog).find('form').eq(0).form('submit', {
              onSubmit: function () {
                var isValid = $(this).form('validate');
                if (!isValid) {
                  return false;
                }
                $.messager.progress({ text: '处理中，请稍候...' });
                $.post("<{:U('admin/operation/set_cp_info')}>", $(this).serialize(), function (res) {
                  $.messager.progress('close');
                  if (res.code != '0000') {
                    $.app.method.tip('提示信息', res.info, 'error');
                  }
                  else {
                    $.app.method.tip('提示信息', res.info, 'info');
                    $(that.dialog).dialog('close');
                    that.refresh();
                  }
                }, 'json'
                );
                return false;
              }
            });
          }
        }, {
          text: '取消',
          iconCls: 'icons-arrow-cross',
          handler: function () {
            $(that.dialog).dialog('close');
          }
        }]
      });
    },

    //删除
    delet: function (id) {
      if (typeof (id) !== 'number') {
        $.app.method.tip('提示信息', '请选择删除信息', 'error');
        return false;
      }
      var that = this;
      $.messager.confirm('提示信息', '确定要删除吗？', function (result) {
        if (!result) return false;

        $.messager.progress({ text: '处理中，请稍候...' });
        $.post("<{:U('admin/operation/delete_cp_info')}>", { id: id }, function (res) {
          $.messager.progress('close');

          if (!res.status) {
            $.messager.alert('错误信息', res.info,'error');
            // $.app.method.tip('提示信息', res.info, 'error');
          } else {
            $.app.method.tip('提示信息', res.info, 'info');
            that.refresh();
          }
        }, 'json');
      });
    },


    batch_delete: function () {
      var checkedItems = $(this.datagrid).datagrid('getChecked');
      var ids = [];
      if (checkedItems.length <= 0) return;
      $.each(checkedItems, function (index, item) {
        ids.push(item.company_id);
      });
      var str = ids.join(",");
      var that = this;
      $.messager.confirm('提示信息', '确定要删除吗？', function (result) {
        if (!result) return false;
        $.messager.progress({ text: '处理中，请稍候...' });
        $.post("<{:U('admin/operation/delete_cp_info')}>", { id: str }, function (res) {
          $.messager.progress('close');
          if (!res.status) {
            // $.app.method.tip('提示信息', res.info, 'error');
            $.messager.alert('错误信息', res.info,'error');
          } else {
            $.app.method.tip('提示信息', res.info, 'info');
            that.refresh();
          }
        }, 'json');
      });
    },


    //数据查询
    search: function () {
      var keyword = $("#cp_keyword").val();
      $(this.datagrid).datagrid("load", {
        keyword: keyword
      });
    },

    //导出
    export: function () {
      $.messager.progress({ text: '处理中，请稍候...' });
      $.post('<{:U("admin/System/menuExport")}>', function (res) {
        $.messager.progress('close');

        if (!res.status) {
          $.app.method.tip('提示信息', res.info, 'error');
        } else {
          $.app.method.tip('提示信息', res.info, 'info');
          window.location.href = res.url;
        }
      }, 'json');
    },

    //导入
    import: function () {
      var that = this;
      $.messager.confirm('提示信息', '该操作将清空所有数据，确定要继续吗？', function (result) {
        if (!result) return false;

        $.app.method.upload(
          '<{:U("admin/Upload/import", array('from'=>urlencode('菜单管理')))}>',
          function (json) {    //上传成功回调函数

            $.messager.progress({ text: '处理中，请稍候...' });
            $.post('<{:U('admin / System / menuImport')}>', { filename: json.filename }, function (res) {
              $.messager.progress('close');

              if (!res.status) {
                $.app.method.tip('提示信息', res.info, 'error');
              } else {
                $.app.method.tip('提示信息', res.info, 'info');
                that.refresh();
              }
            }, 'json');
          },
          function (filename) {  //上传验证函数
            if (!filename.match(/\.data$/)) {
              $.app.method.tip('提示信息', '上传文件后缀不允许', 'error');
              return false;
            }
            return true;
          }
        );
      });
    },
    checkSelectorLength: function () {
      var btn = '.toolbar-btn__delete';
      var checkedItems = $(datagrid).datagrid('getChecked');
      var btnEle = $(btn);
      if (checkedItems.length) {
        btnEle.removeClass('is-disabled');
      } else {
        btnEle.hasClass('is-disabled') || btnEle.addClass('is-disabled');
      }
    }
  };
  var datagrid = '#operation_cp_manage';
  $(datagrid).datagrid({
    onLoadSuccess: function (data) {
      OperationCpManageModel.checkSelectorLength();
    },
    onCheck: function (rowIndex, rowData) {
      OperationCpManageModel.checkSelectorLength();
    },
    onUncheck: function (rowIndex, rowData) {
      OperationCpManageModel.checkSelectorLength();
    },
    onCheckAll: function (rowIndex, rowData) {
      OperationCpManageModel.checkSelectorLength();
    },
    onUncheckAll: function (rowIndex, rowData) {
      OperationCpManageModel.checkSelectorLength();
    },
  })

</script>